package com.base.cn.platform.os.controller.planTools;

import com.base.cn.platform.os.common.constants.CacheConstants;
import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.j2cache.CustomCacheChannel;
import com.base.cn.platform.os.common.j2cache.J2CacheRegion;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.planTools.OverallAdvert;
import com.base.cn.platform.os.service.planTools.OverallAdvertBiz;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 全局广告
 *
 * @Author: lxr
 * @Date: 2019/10/18 14:43
 */
@RestController
@RequestMapping("/manage")
public class OverallAdvertController extends BaseController {

    @Autowired
    private OverallAdvertBiz overallAdvertBiz;
    @Autowired
    private CustomCacheChannel customCacheChannel;

    /**
     * 根据类型查询
     *
     * @return
     */
    @RequestMapping("/overallAdvertInfo")
    public OverallAdvert overallAdvertInfo() {
        OverallAdvert entity = (OverallAdvert) customCacheChannel.get(J2CacheRegion.THIRTY_DAY_REGION.getKey(), CacheConstants.OVERALL_ADVERT).getValue();
        if (ObjectUtils.isEmpty(entity)) {
            entity = overallAdvertBiz.findOne("1=1", null);
            if (ObjectUtils.isNotEmpty(entity)) {
                if (StringUtils.isNotEmpty(entity.getAttribute())) {
                    entity.setAttributeMap(gson.fromJson(entity.getAttribute(), new TypeToken<Map>() {
                    }.getType()));
                }
                customCacheChannel.set(J2CacheRegion.THIRTY_DAY_REGION.getKey(), CacheConstants.OVERALL_ADVERT, entity);
            }
        }
        return entity;
    }

    @RequestMapping("saveEntity")
    public Map saveEntity(@RequestBody OverallAdvert overallAdvert) {
        //验证
        Map attributeMap = overallAdvert.getAttributeMap();
        if (StringUtils.isNotEmpty(attributeMap.get("isShow")) && attributeMap.get("isShow").toString().equals("1")) {
            if (StringUtils.isNotEmpty(attributeMap.get("showLocationTop")) && attributeMap.get("showLocationTop").toString().equals("1")) {
                if (StringUtils.isEmpty(attributeMap.get("logoImgByTop"))) {
                    return ResultUtil.ERROR("请上传头部设置图片");
                }
                if (StringUtils.isEmpty(attributeMap.get("chainedTypeByTop"))) {
                    return ResultUtil.ERROR("请选择头部链接类型");
                }
                if (!attributeMap.get("chainedTypeByTop").equals("0") && StringUtils.isEmpty(attributeMap.get("chainedByTop"))) {
                    return ResultUtil.ERROR("请填写头部链接地址");
                }
            }
            if (StringUtils.isNotEmpty(attributeMap.get("showLocationTail")) && attributeMap.get("showLocationTail").toString().equals("1")) {
                if (StringUtils.isEmpty(attributeMap.get("logoImgByTail"))) {
                    return ResultUtil.ERROR("请上传尾部设置图片");
                }
                if (StringUtils.isEmpty(attributeMap.get("chainedTypeByTail"))) {
                    return ResultUtil.ERROR("请选择尾部链接类型");
                }
                if (!attributeMap.get("chainedTypeByTail").equals("0") && StringUtils.isEmpty(attributeMap.get("chainedByTail"))) {
                    return ResultUtil.ERROR("请填尾部链接地址");
                }
            }
        }
        String attribute = gson.toJson(overallAdvert.getAttributeMap());
        overallAdvert.setAttribute(attribute);
        overallAdvert.setAttributeMap(null);
        if (DataUtil.idIsNotNull(overallAdvert.getId())) {
            overallAdvertBiz.updateById(overallAdvert, null);
        } else {
            overallAdvertBiz.save(overallAdvert);
        }
        customCacheChannel.evict(J2CacheRegion.THIRTY_DAY_REGION.getKey(), CacheConstants.OVERALL_ADVERT);
        return ResultUtil.SUCCESS();
    }
}
